/**
 * Created by Administrator on 2017/5/11.
 */
import Vue from 'vue'

//日期格式化
Vue.filter('dateFormatter', function (timestamp, format) {
       if(!timestamp){
         return '';
       }
       let date=new Date(timestamp);
       return date.pattern(format)
})

//百分号格式化
Vue.filter('percentageFormatter', function (number, digits) {
  let result;

  if (digits === null || digits === undefined) {
    digits = 2;
  }

  digits = parseInt(digits);

  if (number === null || number === '' || isNaN(number)) {
    result = '-';
  } else {
    result = Math.round(number * Math.pow(10, digits) * 100) / Math.pow(10, digits) + '%';
  }

  return result;
})

//boolean 类型格式化
Vue.filter('booleanFormatter', function (boolean, trueText, falseText) {
  let result;

  trueText = trueText || 'Yes';
  falseText = falseText || 'No';

  if (boolean === null || boolean === undefined || boolean === '') {
    result = '-';
  } else {
    result = boolean ? trueText : falseText;
  }

  return result;
})

//字节类型格式化
Vue.filter('byteFormatter', function (size) {
  let result;

  switch (true) {
    case (size === null || size === '' || isNaN(size)):
      result = '-';
      break;

    case (size >= 0 && size < 1024):
      result = size + ' B';
      break;

    case (size >= 1024 && size < Math.pow(1024, 2)):
      result = Math.round(size / 1024 * 100) / 100 + ' K';
      break;

    case (size >= Math.pow(1024, 2) && size < Math.pow(1024, 3)):
      result = Math.round(size / Math.pow(1024, 2) * 100) / 100 + ' M';
      break;

    case (size >= Math.pow(1024, 3) && size < Math.pow(1024, 4)):
      result = Math.round(size / Math.pow(1024, 3) * 100) / 100 + ' G';
      break;

    default:
      result = Math.round(size / Math.pow(1024, 4) * 100) / 100 + ' T';
  }

  return result;
})



